"use strict";
cc._RF.push(module, 'b87711VXqlK3r3TuLuMzTPP', 'HttpHelper');
// script/original/Standar/Utils/HttpHelper.js

"use strict";

window.HttpHelper = {
  /**
   * Get方法
   * @param {请求网址} url 
   * @param {回调} callback 
   * @param {头部参数} headParams 
   */
  Get: function Get(url, callback, headParams) {
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function () {
      //如果请求状态为Done的话，则执行回调
      if (xhr.readyState == 4) {
        if (xhr.status >= 200 && xhr.status < 400) {
          var response = xhr.responseText;
          callback(response);
        } else {
          console.log("请求失败:" + xhr.responseText);
          callback(false);
        }
      }
    };

    xhr.open("GET", url, true);

    if (headParams) {
      for (var key in headParams) {
        xhr.setRequestHeader(key, headParams[key]);
      }
    }

    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //console.log(url);

    xhr.send();
  },
  Post: function Post(url, reqData, callback) {
    //拼接请求参数
    var param = "";

    for (var item in reqData) {
      param += item + "=" + reqData[item] + "&";
    } //console.log(param);
    //发起请求


    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4) {
        if (xhr.status >= 200 && xhr.status < 400) {
          var response = xhr.responseText; //console.log(response);        //打印返回数据

          if (response) {
            callback(response);
          } else {
            console.log("返回数据不存在");
            callback(false);
          }
        } else {
          console.log("请求失败:" + xhr.responseText);
          callback(false);
        }
      }
    };

    xhr.open("POST", url, true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send(param); //reqData为字符串形式： "key=value"
  },
  PostJson: function PostJson(url, params, callback, headParams) {
    //发起请求
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4) {
        if (xhr.status >= 200 && xhr.status < 400) {
          var response = xhr.responseText;

          if (response) {
            callback(response);
          } else {
            console.log("返回数据不存在");
            callback(false);
          }
        } else {
          console.log("请求失败:" + xhr.responseText);
          callback(false);
        }
      }
    };

    xhr.open("POST", url, true);
    xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');

    if (headParams) {
      for (var key in headParams) {
        xhr.setRequestHeader(key, headParams[key]);
      }
    }

    xhr.send(JSON.stringify(params));
  },
  Puts: function Puts(url, reqData, Authorization, callback) {
    var json = JSON.stringify(reqData);
    var xhr = new XMLHttpRequest();
    xhr.open("PUT", url, true);
    xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
    xhr.setRequestHeader('Authorization', Authorization);

    xhr.onload = function () {
      var response = xhr.responseText;

      if (xhr.readyState == 4 && xhr.status == "200") {
        callback(response);
      } else {
        console.log("请求失败:" + xhr.responseText);
        callback(false);
      }
    };

    xhr.send(json);
  },

  /**
   * 上传照片到服务器
   * base64:base64的图片，需要包含头部
   * callback(response),response为返回数据
   */
  UploadPicture: function UploadPicture(base64, callback) {
    var t_url = "http://platform.qdmedia.cc/Screen3DFrame/getCaptures";
    var data = {
      'img': ImageHelper.urlEncode(base64)
    };
    HttpHelper.Post(t_url, data, function (response) {
      if (!response) {
        callback("ERROR");
      } else {
        callback(response);
      }
    });
  }
};

cc._RF.pop();